<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Request;
use think\facade\Env;
use app\common\model\SystemConfig as M;
use app\common\service\SystemConfig as S;

class Config extends Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];
	
    // 系统配置
    public function index(){
        if(Request::isPost()){
		   $req=Request::post('','','strip_tags');
		   S::goSave('info',$req);
           return  $this->success('保存成功',Request::root().'/config/index');
        }
		$data=M::get('info');
		//初始化
		$data['print']['deal_qrcode_type']=$data['print']['deal_qrcode_type']??0;
		$data['print']['label_qrcode_type']=$data['print']['label_qrcode_type']??0;
		
        return $this->fetch('',[
            'data' => $data,
        ]);
    }
	
	// 第三方配置
	public function api(){
		if(Request::isPost()){
		   $req=Request::post('','','strip_tags');
           S::goSave('api',$req);
		   return  $this->success('保存成功',Request::root().'/config/api');
		}
		$data=M::get('api');
		return $this->fetch('',[
		    'data' => $data,
		]);
	}
	
	// env 配置
	public function envSet(){
		if(Request::isPost()){
		   $req=Request::post('','','strip_tags');
		   $data="# 配置文件动态生成，如有添加参数，请同步后台模板选项，防止参数丢失";
		   $data.="\r\n\r\n";
		   foreach($req as $key=>$option){
			   if(is_array($option)){
				   $data.="\r\n[".$key."]\r\n";
				   foreach($option as $name=>$val){
					   $data.=$name.' = '.$val."\r\n";
				   }
			   }else{
				   $data.=$key.' = '.$option."\r\n";
			   }
		   }
		   @file_put_contents(root_path().'.env', $data);
		   return  $this->success('保存成功',Request::root().'/config/envSet');
		}
		
		$data=Env::get();
		return $this->fetch('',[
		    'data' => $data,
		]);
	}
}
